package project.base.common.filter

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.SerializerProvider
import org.apache.commons.text.StringEscapeUtils
import java.io.IOException

/**
 * xxs攻击 String & Json 脚本过滤
 * @author: 三味
 * @update: 2022/3/3 15:54
 */
class XssStringJsonSerializer : JsonSerializer<String>() {

    override fun handledType(): Class<String?>? {
        return String::class.java as Class<String?>?
    }

    @Throws(IOException::class)
    override fun serialize(
        value: String?, jsonGenerator: JsonGenerator,
        serializerProvider: SerializerProvider?
    ) {
        if (value != null) {
            val encodedValue = StringEscapeUtils.escapeHtml4(value)
            jsonGenerator.writeString(encodedValue)
        }
    }

}
